<?php

namespace App\Service\UserCenter\v1_3_4;

use App\Model\Moment;
use App\Model\MomentLike;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 我的点赞列表
 */
class MyLikeListService extends BaseService
{
    public function handle()
    {
        $likeInfo = MomentLike::getUserLikeList($this->userId);
        $userIds = array_column($likeInfo, 'user_id');
        $momentIds = array_column($likeInfo, 'moment_id');
        $profiles = UserProfile::getUsersProfile($userIds, true);
        $momentInfo = Moment::getItems($momentIds);
        $ret = ['list' => []]; //'moment_ids' => json_encode($momentIds)
        foreach ($likeInfo as $item) {
            $userId = $item->user_id;
            $userProfile = $profiles[$userId];
            if (!$userProfile) continue;
            $momentItem = $momentInfo[$item->moment_id] ?? [];
            if (!$momentItem) continue;
            $moment = Moment::getMomentTypeContent($momentItem);
            $ret['list'][] = [
                'user_id' => $userId,
                'nick_name' => $userProfile['nick_name'],
                'avatar' => UserProfile::buildAvatar($userProfile['avatar'], 50),
                'like_time' => (string)$item->created_at,
                'moment_content' => $moment['content'],
                'moment_type' => $moment['type'],
                'moment_id' => $momentItem['id']
            ];
        }
        return $ret;
    }
}
